package com.yxw.yx_hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yx_hotel.dto.AddRoomPropertyDto;
import com.yxw.yx_hotel.dto.QueryRoomPropertyDto;
import com.yxw.yx_hotel.entity.YxRoomEntity;
import com.yxw.yx_hotel.entity.YxRoomPropertyEntity;
import com.yxw.yx_hotel.mapper.RoomPropertyMapper;
import com.yxw.yx_hotel.service.RoomPropertyService;
import com.yxw.yx_hotel.utils.PageUtils;
import com.yxw.yx_hotel.utils.UserUtil;
import com.yxw.yx_hotel.vo.RoomPropertyVo;
import com.yxw.yx_hotel.vo.UserTokenVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  宿舍资产管理
 * @author liuxin
 * @date 2024.01.17
 */
@Service
public class RoomPropertyServiceImpl extends ServiceImpl<RoomPropertyMapper, YxRoomPropertyEntity> implements RoomPropertyService {

    @Autowired
    private UserUtil  userUtil;

    @Autowired
    private RoomPropertyMapper roomPropertyMapper;

    /**
     * 新增宿舍资产
     * @param dto
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addRoomProperty(AddRoomPropertyDto dto) {
        UserTokenVo userVo = userUtil.getLoginUserVo();
        YxRoomPropertyEntity entity = new YxRoomPropertyEntity();
        BeanUtil.copyProperties(dto,entity);
        entity.setCreateTime(new Date());
        entity.setCreateName(userVo.getNickName());
        entity.setTenantId(userVo.getTenantId());
        entity.setCreateId(userVo.getUserId());
        baseMapper.insert(entity);
    }

    /**
     * 查询宿舍资产列表/分页
     * @param dto
     * @return
     */
    @Override
    public PageVO<YxRoomPropertyEntity> pageRoomPropertyList(QueryRoomPropertyDto dto) {
        UserTokenVo userVo = userUtil.getLoginUserVo();
        Page<YxRoomPropertyEntity> page = new Page<>(dto.getCurrPage(),dto.getPageSize());
        QueryWrapper<YxRoomPropertyEntity>  queryWrapper=new QueryWrapper();
        queryWrapper.eq("room_id",dto.getRoomId());
        queryWrapper.eq("tenant_id",userVo.getTenantId());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.select("yx_room_property.*","1 as repairCnt","null as lastRepairTime");
        Page<YxRoomPropertyEntity> pageEntity = baseMapper.selectPage(page, queryWrapper);
        return PageUtils.get(pageEntity);
    }
    /**
     * 查询宿舍资产列表
     * @param dto
     * @return
     */
    @Override
    public List<YxRoomPropertyEntity> roomPropertyList(QueryRoomPropertyDto dto) {
        UserTokenVo userVo = userUtil.getLoginUserVo();
        QueryWrapper<YxRoomPropertyEntity>  queryWrapper=new QueryWrapper();
        queryWrapper.eq("room_id",dto.getRoomId());
        queryWrapper.eq("tenant_id",userVo.getTenantId());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.select("yx_room_property.*","1 as repairCnt","null as lastRepairTime");
        List<YxRoomPropertyEntity> records = baseMapper.selectList(queryWrapper);
        return records;
    }    
}
